import com.tj.common.util.test.HttpTestUtil;

public class Main {

    static String[] values = new String[] { "t", "saround", "cx", "220987664", "cy", "101846004", "range", "100000", "size", "100", "page", "3", "v", "2",
            "div", "600801b01", "dip", "10600", "dic", "C2340", "ditu", "000000000000000", "s", "餐馆" };

    public static void main(String[] args) throws Exception {
        byte[] buf = HttpTestUtil.httpPostForByte("http://m.mapabc.com/MinimapService/MPS", values, null);
        System.out.println("-----------");
        System.out.println(buf.length);
        dealWithData(buf);
    }

    public static void dealWithData(byte[] bytes) {
        for (int i = 0; i < bytes.length; i++) {
            System.out.print(i + ":" + bytes[i] + "  ");
        }
        System.out.println("Error code " + bytes[9]);
        System.out.println("cenX " + getInt(bytes, 14));
        System.out.println("cenY " + getInt(bytes, 18));
        System.out.println("totalPage " + getInt(bytes, 22));
        System.out.println("resultcount " + getInt(bytes, 26));
        int resultCount = getInt(bytes, 26);
        int entryStartIndex = 26 + 4;
        for (int i = 0; i < resultCount; i++) {            
            System.out.println("-------------entry " + i + "------------------");
            System.out.println("entry start index " + entryStartIndex);
            int poiDataLength = getInt(bytes, entryStartIndex);
            System.out.println("poi data length " + poiDataLength);

            /// id
            int idLengthIndex = entryStartIndex + 4;
            int idLength = getShort(bytes, idLengthIndex);
            System.out.println("id length " + idLength);

            int idIndex = idLengthIndex + 2;
            String poiid = new String(getSubbytes(bytes, idIndex, idLength));
            System.out.println(poiid);

            /// name
            int nameLengthIndex = idLengthIndex + idLength + 2;
            int nameLength = getShort(bytes, nameLengthIndex);
            System.out.println("name length " + nameLength);

            int nameIndex = nameLengthIndex + 2;
            String name = new String(getSubbytes(bytes, nameIndex, nameLength));
            System.out.println("name " + name);

            /// address
            int addressLengthIndex = nameLengthIndex + nameLength + 2;
            int addressLength = getShort(bytes, addressLengthIndex);
            System.out.println("address length " + addressLength);

            int addressIndex = addressLengthIndex + 2;
            String address = new String(getSubbytes(bytes, addressIndex, addressLength));
            System.out.println("address " + address);

            /// tel
            int telLengthIndex = addressLengthIndex + addressLength + 2;
            int telLength = getShort(bytes, telLengthIndex);
            System.out.println("tel length " + telLength);

            int telIndex = telLengthIndex + 2;
            String tel = new String(getSubbytes(bytes, telIndex, telLength));
            System.out.println("tel " + tel);

            int Xindex = telLengthIndex + telLength + 2;
            int x = getInt(bytes, Xindex);
            System.out.println("x " + x);

            int Yindex = Xindex + 4;
            int y = getInt(bytes, Yindex);
            System.out.println("y " + y);

            int distanceIndex = Yindex + 4;
            int distance = getInt(bytes, distanceIndex);
            System.out.println("distance " + distance);

            /// citycode
            int cityLengthIndex = distanceIndex + 4;
            int cityLength = getShort(bytes, cityLengthIndex);
            System.out.println("city length " + cityLength);

            int cityIndex = cityLengthIndex + 2;
            String city = new String(getSubbytes(bytes, cityIndex, cityLength));
            System.out.println("city " + city);

            entryStartIndex = entryStartIndex + poiDataLength + 4;

        }
    }

    static int getInt(byte[] bytes, int offset) {
        int v0 = bytes[offset + 3] & 0xff;
        int v1 = bytes[offset + 2] & 0xff;
        int v2 = bytes[offset + 1] & 0xff;
        int v3 = bytes[offset + 0] & 0xff;
        v0 = (v0 << 0x18) + (v1 << 0x10) + (v2 << 8) + v3;
        return v0;
    }

    static int getShort(byte[] bytes, int offset) {
        int v2 = bytes[offset + 1] & 0xff;
        int v3 = bytes[offset + 0] & 0xff;
        return (v2 << 8) + v3;
    }

    static byte[] getSubbytes(byte[] bytes, int start, int length) {
        byte[] r = new byte[length];
        System.arraycopy(bytes, start, r, 0, length);
        return r;
    }
}
